
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by inu1255 on 2016/11/17.
 */


public class Main {
    public static String[] xk = {"语文","数学","英语","物理","化学","生物","政治","历史","地理"};
    public static void load(Workbook wb,String name) throws Exception {
        FileOutputStream os = new FileOutputStream("m.sql",true);
        int ns = wb.getNumberOfSheets();
        String prev = "";
        for (int i=0;i<ns;i++){
            Sheet s = wb.getSheetAt(i);
            String value = null;
            for (int j = s.getNumMergedRegions() - 1; j >= 0; j--) {
                CellRangeAddress region = s.getMergedRegion(j);
                Row firstRow = s.getRow(region.getFirstRow());
                Cell firstCellOfFirstRow = firstRow.getCell(region.getFirstColumn());

                if (firstCellOfFirstRow.getCellType() == Cell.CELL_TYPE_STRING) {
                    value = firstCellOfFirstRow.getStringCellValue();
                }

                s.removeMergedRegion(j);

                for (Row row : s) {
                    for (Cell cell : row) {
                        if (region.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            cell.setCellValue(value);
                        }
                    }
                }

            }
            String[] ss = new String[20];
            int row=0;
            for (Row cells : s) {
                int col=0;
                for (Cell cell : cells) {
                    if(col>1)break;
                    if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)continue;
                    String str = cell.getCellType()==Cell.CELL_TYPE_NUMERIC?cell.getNumericCellValue()+"":cell.getStringCellValue();
                    str = str.trim();
//                    System.out.println(row+":"+str);
                    if(str.equals("编号")||str.equals("序号")){
                        row=0;
                        break;
                    }
                    if(ss[0]==null){
                        str = str.replaceAll("目录","");
                        String[] split;
                        for (String s1 : xk) {
                            split = str.split(s1);
                            if(split.length>1){
                                ss[0] = "\""+s1+"\",\""+split[0]+"\",\""+split[1]+"\"";
                                break;
                            }
                        }
                        if(ss[0]!=null)break;
                    }else if(!str.isEmpty()){
                        ss[col+1] = "\""+str.replaceAll("\"","\\\"")+"\"";
                        col++;
                    }
                }
                //System.out.println(col);
                if(row>0){
                    String line = "";
                    String insert = "insert into category1 (c2,c3,c4";
                    int c = 5;
                    for (String s1 : ss) {
                        if(col--<0)break;
                        if(s1!=null){
                            line+=s1+",";
                            if(c==5)c=6;
                            else insert+=",c"+c++;
                        }
                    }
                    if(!prev.equals(line)&&!line.isEmpty()){
                        prev=line;
                        line =insert+") values("+(line.substring(0,line.length()-1))+");\n";
                        os.write(line.getBytes());
                    }else{
//                        System.out.println(line);
                    }
                }
                row++;
            }
        }
        os.close();
    }
    public static void main(String[] args) throws Exception {
        new File("m.sql").delete();
//        File m = new File("m");
//        dfs(m);
        dfs(new File("m/8 历史/岳麓版 历史必修3知识点.xlsx"));
    }

    private static void dfs(File file) throws Exception   {
        if(file.isDirectory()){
            for (File file1 : file.listFiles()) {
                dfs(file1);
            }
        }else {
            readFile(file);
        }
    }

    private static void readFile(File file1) throws Exception  {
        FileInputStream fs = new FileInputStream(file1);
        String filename = file1.getName();
        System.out.println(filename);
        Workbook wb;
        if(filename.endsWith("xlsx")){
            wb = new XSSFWorkbook(fs);
        }else if(filename.endsWith("xls")){
            wb = new HSSFWorkbook(fs);
        }else{
            fs.close();
            return;
        }
        String s = filename.replaceAll("\\.\\w+", "") + ".txt";
        load(wb,s);
        fs.close();
    }
}
